package com.lxujia.ruankao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lxujia.ruankao.R;
import com.lxujia.ruankao.model.CategoryItem;

import java.util.List;

public class CategoryListViewAdapter extends BaseAdapter {
    private Context mContext;
    private List<CategoryItem> mList;
    private List<Integer> mListCount;
    private boolean mShowCount;

    public CategoryListViewAdapter(Context context, boolean showCount) {
        this.mContext = context;
        this.mShowCount = showCount;
    }

    public void setData(List<CategoryItem> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    public void setCountData(List<Integer> listCount) {
        this.mListCount = listCount;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view==null){
            vh = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.category_list_item, null);
            vh.title = (TextView) view.findViewById(R.id.category_period_tv);
            vh.doneTx = (TextView) view.findViewById(R.id.category_question_done);
            vh.allQuestionTx = (TextView) view.findViewById(R.id.category_question_all);
            view.setTag(vh);
        }else {
            vh = (ViewHolder) view.getTag();
        }

        CategoryItem categoryItem = mList.get(i);
        String periodToShow = categoryItem.getPeriodToShow();
        String extInfo = categoryItem.getExtInfo();

        vh.title.setText(String.format("%s(%s)", periodToShow, extInfo));
        if (mShowCount && mListCount != null && mListCount.get(i) != null) {
            vh.doneTx.setText(String.valueOf(mListCount.get(i)));
            vh.doneTx.setVisibility(View.VISIBLE);
            vh.allQuestionTx.setVisibility(View.VISIBLE);
        } else {
            vh.doneTx.setVisibility(View.GONE);
            vh.allQuestionTx.setVisibility(View.GONE);
        }

        return view;
    }
    public class ViewHolder{
        TextView title;
        TextView doneTx;
        TextView allQuestionTx;
    }
}
